package com.t.bricks.bussiness.controller.getui;

import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.api.StatisticApi;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.getui.push.v2.sdk.dto.res.CidStatusDTO;
import com.getui.push.v2.sdk.dto.res.statistic.StatisticDTO;

/**
 * 个推
 */
public class GetuiController {

	/**
	 * 用于创建接口对象
	 */
	private ApiHelper apiHelper = null;

	private PushApi pushApi = null;

	private UserApi userApi = null;

	private StatisticApi statisticApi = null;
	
	private PushApi getPushApi() {
		if (null != pushApi) {
			return pushApi;
		}
		
		pushApi = apiHelper.creatApi(PushApi.class);
		
		return pushApi;
	}
	
	private UserApi getUserApi() {
		if (null != userApi) {
			return userApi;
		}
		
		userApi = apiHelper.creatApi(UserApi.class);
		
		return userApi;
	}
	
	private StatisticApi getStatisticApi() {
		if (null != statisticApi) {
			return statisticApi;
		}
		
		statisticApi = apiHelper.creatApi(StatisticApi.class);
		
		return statisticApi;
	}

	public static void main(String[] args) {
		GetuiController controller = new GetuiController();
		controller.creatApi();
		
		controller.pushByCid("198c8d1434e46af8016777f88122766c8");
	}

	/**
	 * 创建API
	 */
	private void creatApi() {
		GtApiConfiguration apiConfiguration = new GtApiConfiguration();
		// 填写应用配置
		apiConfiguration.setAppId("kZjaAtImn96kvHQvC2Uo25");
		apiConfiguration.setAppKey("OyXQ4OiWN78FDQ2yNI6r46");
		apiConfiguration.setMasterSecret("AZUeQKwHEg7A6vhGN5iPG3");
		// 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
		apiConfiguration.setDomain("https://restapi.getui.com/v2/");
		// 实例化ApiHelper对象，用于创建接口对象
		apiHelper = ApiHelper.build(apiConfiguration);
	}

	/**
	 * 获取单日推送数据
	 */
	public void lookStatistic() {
		Set<String> taskIds = new HashSet<String>();
		taskIds.add("xxx");
		ApiResult<Map<String, Map<String, StatisticDTO>>> apiResult = getStatisticApi()
				.queryPushResultByTaskIds(taskIds);
		if (apiResult.isSuccess()) {
			// success
			System.out.println(apiResult.getData());
		} else {
			// failed
			System.out.println("code:" + apiResult.getCode() + ", msg: "
					+ apiResult.getMsg());
		}
	}

	/**
	 * 根据cid进行单推
	 * https://docs.getui.com/getui/server/rest_v2/common_args/?id=doc-title-6
	 * @param cid
	 */
	public void pushByCid(String cid) {
		// 根据cid进行单推
		PushDTO<Audience> pushDTO = new PushDTO<Audience>();
		// 设置推送参数
		pushDTO.setRequestId(System.currentTimeMillis() + "");
		PushMessage pushMessage = new PushMessage();
		pushDTO.setPushMessage(pushMessage);
		
		//pushMessage.setTransmission("请填写你的透传消息内容");//透传方式,失败
		
		GTNotification notification = new GTNotification();
		pushMessage.setNotification(notification);
		
		//notification.setTitle("标题" + new Date());
		notification.setTitle("测试" + new Date());
		notification.setBody("来自PC的推送");
		notification.setClickType("startapp");
		//notification.setUrl("http://blog.tomatomeatman.icu");
		
		
//		notification.setTitle("个推" + new Date());
//		//notification.setBody("这里是测试,这里是测试");
//		notification.setBody("body");
		
//		notification.setClickType("startapp");
		
//		notification.setClickType("payload");
//		notification.setPayload("这是使用了自定义消息内容");
//		ClickType: 点击通知后续动作,
//		目前支持以下后续动作，
//		intent：打开应用内特定页面(厂商都支持)，
//		url：打开网页地址(厂商都支持，华为要求https协议)，
//		payload：自定义消息内容启动应用(华为和oppo不支持，其他厂商支持)，
//		startapp：打开应用首页(厂商都支持)
		
//		notification.setClickType("url");
//		notification.setUrl("http://blog.tomatomeatman.icu");
		
		// 设置接收人信息
		Audience audience = new Audience();
		pushDTO.setAudience(audience);
		audience.addCid(cid);

		// 进行cid单推
		ApiResult<Map<String, Map<String, String>>> apiResult = getPushApi()
				.pushToSingleByCid(pushDTO);
		if (apiResult.isSuccess()) {
			// success
			System.out.println(apiResult.getData());
		} else {
			// failed
			System.out.println("code:" + apiResult.getCode() + ", msg: "
					+ apiResult.getMsg());
		}
	}

	/**
	 * 查询用户状态
	 */
	public void findUserStatus() {
		Set<String> cids = new HashSet<String>();
		cids.add("xxx");
		// 查询用户状态
		ApiResult<Map<String, CidStatusDTO>> apiResult = getUserApi()
				.queryUserStatus(cids);
		if (apiResult.isSuccess()) {
			// success
			System.out.println(apiResult.getData());
		} else {
			// failed
			System.out.println("code:" + apiResult.getCode() + ", msg: "
					+ apiResult.getMsg());
		}
	}

}
